package com.loongtech.bi.manager.report;

import com.loongtech.bi.action.PageHelper;
import com.loongtech.bi.action.UIHelper;
import com.loongtech.bi.entity.pay.EntityNewUserLTV;
import com.loongtech.bi.entity.pay.EntityPayUser;
import com.loongtech.bi.entity.player.EntityDau;
import com.loongtech.bi.entity.player.EntitySecondLeft;
import com.loongtech.bi.entity.player.EntitySevenLeft;
import com.loongtech.bi.entity.player.EntityThirdLeft;
import com.loongtech.bi.manager.pay.FirstPayUserManager;
import com.loongtech.bi.manager.pay.NewUserLTVManager;
import com.loongtech.bi.manager.pay.PayUserManager;
import com.loongtech.bi.manager.pay.TotalCashManager;
import com.loongtech.bi.manager.player.*;
import com.loongtech.bi.support.chart.ChartData1;
import com.loongtech.bi.support.chart.ChartFlow;
import com.loongtech.bi.support.chart.ChartSelect;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.Map.Entry;

/**
 * 运营报表-概况manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class OverviewReportManager extends ManagerQueryCacheBase<Object> {
	private static final long serialVersionUID = 1L;

	@Autowired
	AcuManager acuManager;
	
	@Autowired
	PcuManager pcuManager;
	
	@Autowired
	LeftManager leftManager;
	
	@Autowired
	DauManager dauManager;
	
	@Autowired
	NewUserManager newUserManager;
	
	@Autowired
	DaPlayTimeManager daPlayTimeManager;
	
	@Autowired
	TotalCashManager totalCashManager;
	
	@Autowired
	PayUserManager payUserManager;
	
	@Autowired
	FirstPayUserManager firstPayUserManager;

    @Autowired
    ActiveDeviceManager activeDeviceManager;

    @Autowired
    NewDeviceManager newDeviceManager;

    @Autowired
    NewUserLTVManager newUserLTVManager;

    // Integer
    private String getRadio(String yd, String nd) {
        if ("-".equals(yd) || "-".equals(nd)) {
            return "";
        }

        int n = Integer.parseInt(yd);
        int y = Integer.parseInt(nd);
        int d = n - y;

        if (d == 0) {
            return nd + "(0%)";
        } else {
            if (y == 0) {
                return nd + "(" + d * 100 + "%)";
            } else {
                return nd + "(" + PageHelper.double2Percent(d * 1.0 / y) + ")";
            }
        }
    }

    // Long
    private String getRadio1(String yd, String nd) {
        if ("-".equals(yd) || "-".equals(nd)) {
            return "";
        }

        long n = Long.parseLong(yd);
        long y = Long.parseLong(nd);
        long d = n - y;

        if (d == 0) {
            return nd + "(0%)";
        } else {
            if (y == 0) {
                return nd + "(" + d * 100 + "%)";
            } else {
                return nd + "(" + PageHelper.double2Percent(d * 1.0 / y) + ")";
            }
        }
    }

    // %
    private String getRadio2(String yd, String nd) {
        if ("-".equals(yd) || "-".equals(nd) || "".equals(yd) || "".equals(nd)) {
            return "";
        }

        float n = Float.parseFloat(yd.substring(0, yd.indexOf("%")));
        float y = Float.parseFloat(nd.substring(0, nd.indexOf("%")));
        float d = n - y;

        if (d == 0) {
            return nd + "(0%)";
        } else {
            if (y == 0) {
                return nd + "(" + d * 100 + "%)";
            } else {
                return nd + "(" + PageHelper.double2Percent(d * 1.0 / y) + ")";
            }
        }
    }

    // Float
    private String getRadio3(String yd, String nd) {
        if ("-".equals(yd) || "-".equals(nd)) {
            return "";
        }

        float n = Float.parseFloat(yd);
        float y = Float.parseFloat(nd);
        float d = n - y;

        if (d == 0) {
            return nd + "(0%)";
        } else {
            if (y == 0) {
                return nd + "(" + d * 100 + "%)";
            } else {
                return nd + "(" + PageHelper.double2Percent(d * 1.0 / y) + ")";
            }
        }
    }

    public List<YesterDayOverviewData> getYesterdayOverviewData(String queryPlatform, String queryChannel, String queryServer) {
        List<YesterDayOverviewData> result = new ArrayList<>();

        YesterDayOverviewData y1 = new YesterDayOverviewData();
        y1.setTitle("昨日数据");
        result.add(y1);
        YesterDayOverviewData y2 = new YesterDayOverviewData();
        y2.setTitle("环比变化率");
        result.add(y2);
        YesterDayOverviewData y3 = new YesterDayOverviewData();
        y3.setTitle("周同比变化率");
        result.add(y3);

        Date yesterday = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));
        Date qiantian = new Date(Time.getBeginningOfDay(Time.currentTime() - 2 * Time.DAY));
        Date shangzhou = new Date(Time.getBeginningOfDay(Time.currentTime() - 8 * Time.DAY));

        // 激活设备
        List<Object[]> list00 = activeDeviceManager.getActiveDeviceByAllKind(yesterday, yesterday, queryPlatform, queryChannel, queryServer);
        if (list00 != null && list00.size() > 0 && list00.get(0)[0] != null) {
            int sumActiveAddDevice = 0;
            for(Object[] o : list00) {
                sumActiveAddDevice += Integer.parseInt(o[1].toString());
            }
            y1.setActiveDevice(sumActiveAddDevice + "");
            y1.setActiveDeviceRadio(y1.getActiveDevice());
        }
        List<Object[]> list01 = activeDeviceManager.getActiveDeviceByAllKind(qiantian, qiantian, queryPlatform, queryChannel, queryServer);
        if (list01 != null && list01.size() > 0 && list01.get(0)[0] != null) {
            int sumActiveAddDevice = 0;
            for(Object[] o : list01) {
                sumActiveAddDevice += Integer.parseInt(o[1].toString());
            }
            y2.setActiveDevice(sumActiveAddDevice + "");
            y2.setActiveDeviceRadio(getRadio(y1.getActiveDevice(), y2.getActiveDevice()));
        }
        List<Object[]> list02 = activeDeviceManager.getActiveDeviceByAllKind(shangzhou, shangzhou, queryPlatform, queryChannel, queryServer);
        if (list02 != null && list02.size() > 0 && list02.get(0)[0] != null) {
            int sumActiveAddDevice = 0;
            for(Object[] o : list02) {
                sumActiveAddDevice += Integer.parseInt(o[1].toString());
            }
            y3.setActiveDevice(sumActiveAddDevice + "");
            y3.setActiveDeviceRadio(getRadio(y1.getActiveDevice(), y3.getActiveDevice()));
        }


        // 新增设备
        List<Object[]> list001 = newDeviceManager.getNewDeviceByAllKind(yesterday, yesterday, queryPlatform, queryChannel, queryServer);
        if (list001 != null && list001.size() > 0 && list001.get(0)[0] != null) {
            int sumNewAddDevice = 0;
            for(Object[] o : list001) {
                sumNewAddDevice += Integer.parseInt(o[1].toString());
            }
            y1.setNewDevice(sumNewAddDevice + "");
            y1.setNewDeviceRadio(y1.getNewDevice());
        }
        List<Object[]> list002 = newDeviceManager.getNewDeviceByAllKind(qiantian, qiantian, queryPlatform, queryChannel, queryServer);
        if (list002 != null && list002.size() > 0 && list002.get(0)[0] != null) {
            int sumNewAddDevice = 0;
            for(Object[] o : list002) {
                sumNewAddDevice += Integer.parseInt(o[1].toString());
            }
            y2.setNewDevice(sumNewAddDevice + "");
            y2.setNewDeviceRadio(getRadio(y1.getNewDevice(), y2.getNewDevice()));
        }
        List<Object[]> list003 = newDeviceManager.getNewDeviceByAllKind(shangzhou, shangzhou, queryPlatform, queryChannel, queryServer);
        if (list003 != null && list003.size() > 0 && list003.get(0)[0] != null) {
            int sumNewAddDevice = 0;
            for(Object[] o : list003) {
                sumNewAddDevice += Integer.parseInt(o[1].toString());
            }
            y3.setNewDevice(sumNewAddDevice + "");
            y3.setNewDeviceRadio(getRadio(y1.getNewDevice(), y3.getNewDevice()));
        }


        // 新增账号数
        List<Object[]> list10 = newUserManager.getNewUserByAllKind(yesterday, yesterday, queryPlatform, queryChannel, queryServer);
        if (list10 != null && list10.size() > 0 && list10.get(0)[0] != null) {
            int sumNewAddUser = 0;
            for(Object[] o : list10) {
                sumNewAddUser += Integer.parseInt(o[1].toString());
            }
            y1.setNewUser(sumNewAddUser + "");
            y1.setNewUserRadio(y1.getNewUser());
        }
        List<Object[]> list11 = newUserManager.getNewUserByAllKind(qiantian, qiantian, queryPlatform, queryChannel, queryServer);
        if (list11 != null && list11.size() > 0 && list11.get(0)[0] != null) {
            int sumNewAddUser = 0;
            for(Object[] o : list11) {
                sumNewAddUser += Integer.parseInt(o[1].toString());
            }
            y2.setNewUser(sumNewAddUser + "");
            y2.setNewUserRadio(getRadio(y1.getNewUser(), y2.getNewUser()));
        }
        List<Object[]> list12 = newUserManager.getNewUserByAllKind(shangzhou, shangzhou, queryPlatform, queryChannel, queryServer);
        if (list12 != null && list12.size() > 0 && list12.get(0)[0] != null) {
            int sumNewAddUser = 0;
            for(Object[] o : list12) {
                sumNewAddUser += Integer.parseInt(o[1].toString());
            }
            y3.setNewUser(sumNewAddUser + "");
            y3.setNewUserRadio(getRadio(y1.getNewUser(), y3.getNewUser()));
        }

        // 活跃账号数
        List<EntityDau> list20 = dauManager.getAllDauByAllKind(yesterday, yesterday, queryPlatform, queryChannel, queryServer);
        for(EntityDau o : list20) {
            y1.setActiveUser(o.getDau() + "");
            y1.setActiveUserRadio(y1.getActiveUser());
        }
        List<EntityDau> list21 = dauManager.getAllDauByAllKind(qiantian, qiantian, queryPlatform, queryChannel, queryServer);
        for(EntityDau o : list21) {
            y2.setActiveUser(o.getDau() + "");
            y2.setActiveUserRadio(getRadio(y1.getActiveUser(), y2.getActiveUser()));
        }
        List<EntityDau> list22 = dauManager.getAllDauByAllKind(shangzhou, shangzhou, queryPlatform, queryChannel, queryServer);
        for(EntityDau o : list22) {
            y3.setActiveUser(o.getDau() + "");
            y3.setActiveUserRadio(getRadio(y1.getActiveUser(), y3.getActiveUser()));
        }

        // 付费金额
        List<Object[]> list30  = totalCashManager.getTotalCashByAllKind(yesterday, yesterday, queryPlatform, queryChannel, queryServer);
        if (list30 != null && list30.size() > 0 && list30.get(0)[0] != null) {
            long sumCashAdd = 0;
            for(Object[] o : list30) {
                sumCashAdd += Long.parseLong(o[1].toString());
            }

            y1.setPay(UIHelper.getYuan(sumCashAdd) + "");
            y1.setPayRadio(y1.getPay());
        }
        List<Object[]> list31  = totalCashManager.getTotalCashByAllKind(qiantian, qiantian, queryPlatform, queryChannel, queryServer);
        if (list31 != null && list31.size() > 0 && list31.get(0)[0] != null) {
            long sumCashAdd = 0;
            for(Object[] o : list31) {
                sumCashAdd += Long.parseLong(o[1].toString());
            }
            y2.setPay(UIHelper.getYuan(sumCashAdd) + "");
            y2.setPayRadio(getRadio1(y1.getPay(), y2.getPay()));
        }
        List<Object[]> list32  = totalCashManager.getTotalCashByAllKind(shangzhou, shangzhou, queryPlatform, queryChannel, queryServer);
        if (list32 != null && list32.size() > 0 && list32.get(0)[0] != null) {
            long sumCashAdd = 0;
            for(Object[] o : list32) {
                sumCashAdd += Long.parseLong(o[1].toString());
            }
            y3.setPay(UIHelper.getYuan(sumCashAdd) + "");
            y3.setPayRadio(getRadio1(y1.getPay(), y3.getPay()));
        }

        // 付费账号数
        List<EntityPayUser> list40  = payUserManager.getAllPayUserByAllKind(yesterday, yesterday, queryPlatform, queryChannel, queryServer);
        for(EntityPayUser o : list40) {
            y1.setPayUser(o.getAddcash_user() + "");
            y1.setPayUserRadio(y1.getPayUser());
        }
        List<EntityPayUser> list41  = payUserManager.getAllPayUserByAllKind(qiantian, qiantian, queryPlatform, queryChannel, queryServer);
        for(EntityPayUser o : list41) {
            y2.setPayUser(o.getAddcash_user() + "");
            y2.setPayUserRadio(getRadio(y1.getPayUser(), y2.getPayUser()));
        }
        List<EntityPayUser> list42  = payUserManager.getAllPayUserByAllKind(shangzhou, shangzhou, queryPlatform, queryChannel, queryServer);
        for(EntityPayUser o : list42) {
            y3.setPayUser(o.getAddcash_user() + "");
            y3.setPayUserRadio(getRadio(y1.getPayUser(), y3.getPayUser()));
        }

        // 活跃付费率
        if (!"-".equals(y1.getPayUser()) && !"-".equals(y1.getActiveUser()) && !"0".equals(y1.getActiveUser())) {
            y1.setPayOnActiveUserRadio(PageHelper.double2Percent(Integer.parseInt(y1.getPayUser()) * 1.0 / Integer.parseInt(y1.getActiveUser())));
            y1.setPayOnActiveUserRadioRadio(y1.getPayOnActiveUserRadio());
        }
        if (!"-".equals(y2.getPayUser()) && !"-".equals(y2.getActiveUser()) && !"0".equals(y2.getActiveUser())) {
            String yd2 = PageHelper.double2Percent(Integer.parseInt(y2.getPayUser()) * 1.0 / Integer.parseInt(y2.getActiveUser()));
            y2.setPayOnActiveUserRadio(yd2);
            y2.setPayOnActiveUserRadioRadio(getRadio2(y1.getPayOnActiveUserRadio(), yd2));
        }
        if (!"-".equals(y3.getPayUser()) && !"-".equals(y3.getActiveUser()) && !"0".equals(y3.getActiveUser())) {
            String yd3 = PageHelper.double2Percent(Integer.parseInt(y3.getPayUser()) * 1.0 / Integer.parseInt(y3.getActiveUser()));
            y3.setPayOnActiveUserRadio(yd3);
            y3.setPayOnActiveUserRadioRadio(getRadio2(y1.getPayOnActiveUserRadio(), yd3));
        }

        // 付费ARPU
        if (!"-".equals(y1.getPayUser()) && !"-".equals(y1.getPay()) && !"0".equals(y1.getPayUser())) {
            y1.setPayArpu(PageHelper.floatKeepTwoNumber((float)(Integer.parseInt(y1.getPay()) * 1.0 / Integer.parseInt(y1.getPayUser()))) + "");
            y1.setPayArpuRadio(y1.getPayArpu());
        }
        if (!"-".equals(y2.getPayUser()) && !"-".equals(y2.getPay()) && !"0".equals(y2.getPayUser())) {
            String yd2 = PageHelper.floatKeepTwoNumber((float)(Integer.parseInt(y2.getPay()) * 1.0 / Integer.parseInt(y2.getPayUser()))) + "";
            y2.setPayArpu(yd2);
            y2.setPayArpuRadio(getRadio3(y1.getPayArpu(), yd2));
        }
        if (!"-".equals(y3.getPayUser()) && !"-".equals(y3.getPay()) && !"0".equals(y3.getPayUser())) {
            String yd3 = PageHelper.floatKeepTwoNumber((float)(Integer.parseInt(y3.getPay()) * 1.0 / Integer.parseInt(y3.getPayUser()))) + "";
            y3.setPayArpu(yd3);
            y3.setPayArpuRadio(getRadio3(y1.getPayArpu(), yd3));
        }

        // 活跃ARPU
        if (!"-".equals(y1.getActiveUser()) && !"-".equals(y1.getPay()) && !"0".equals(y1.getActiveUser())) {
            y1.setActiveArpu(PageHelper.floatKeepTwoNumber((float)(Integer.parseInt(y1.getPay()) * 1.0 / Integer.parseInt(y1.getActiveUser()))) + "");
            y1.setActiveArpuRadio(y1.getActiveArpu());
        }
        if (!"-".equals(y2.getActiveUser()) && !"-".equals(y2.getPay()) && !"0".equals(y2.getActiveUser())) {
            String yd2 = PageHelper.floatKeepTwoNumber((float)(Integer.parseInt(y2.getPay()) * 1.0 / Integer.parseInt(y2.getActiveUser()))) + "";
            y2.setActiveArpu(yd2);
            y2.setActiveArpuRadio(getRadio3(y1.getActiveArpu(), yd2));
        }
        if (!"-".equals(y3.getActiveUser()) && !"-".equals(y3.getPay()) && !"0".equals(y3.getPayUser())) {
            String yd3 = PageHelper.floatKeepTwoNumber((float)(Integer.parseInt(y3.getPay()) * 1.0 / Integer.parseInt(y3.getActiveUser()))) + "";
            y3.setActiveArpu(yd3);
            y3.setActiveArpuRadio(getRadio3(y1.getActiveArpu(), yd3));
        }

        // 激活新增设备率
        if (!"-".equals(y1.getActiveDevice()) && !"-".equals(y1.getNewDevice()) && !"0".equals(y1.getActiveDevice())) {
            y1.setNewOnActiveDevcieRadio(PageHelper.double2Percent(Integer.parseInt(y1.getNewDevice()) * 1.0 / Integer.parseInt(y1.getActiveDevice())));
            y1.setNewOnActiveDevcieRadioRadio(y1.getNewOnActiveDevcieRadio());
        }
        if (!"-".equals(y2.getActiveDevice()) && !"-".equals(y2.getNewDevice()) && !"0".equals(y2.getActiveDevice())) {
            String yd2 = PageHelper.double2Percent(Integer.parseInt(y2.getNewDevice()) * 1.0 / Integer.parseInt(y2.getActiveDevice()));
            y2.setNewOnActiveDevcieRadio(yd2);
            y2.setNewOnActiveDevcieRadioRadio(getRadio2(y1.getNewOnActiveDevcieRadio(), yd2));
        }
        if (!"-".equals(y3.getActiveDevice()) && !"-".equals(y3.getNewDevice()) && !"0".equals(y3.getActiveDevice())) {
            String yd3 = PageHelper.double2Percent(Integer.parseInt(y3.getNewDevice()) * 1.0 / Integer.parseInt(y3.getActiveDevice()));
            y3.setNewOnActiveDevcieRadio(yd3);
            y3.setNewOnActiveDevcieRadioRadio(getRadio2(y1.getNewOnActiveDevcieRadio(), yd3));
        }

        return result;
    }

	public List<OverviewData> getOverviewData(Date dateStart, Date dateEnd) {
		List<OverviewData> result = new ArrayList<>();
		
		// 新增账号数
		OverviewData o1 = new OverviewData();
		o1.setTitle("新增账号数");
		// 活跃账号数
		OverviewData o2 = new OverviewData();
		o2.setTitle("活跃账号数");
		// 付费金额
		OverviewData o3 = new OverviewData();
		o3.setTitle("付费金额");
		// 付费账号数
		OverviewData o4 = new OverviewData();
		o4.setTitle("付费账号数");
		// 活跃付费渗透率
		OverviewData o5 = new OverviewData();
		o5.setTitle("活跃付费率");
		// 付费ARPU
		OverviewData o6 = new OverviewData();
		o6.setTitle("付费ARPU");
		
		List<Object[]> list1 = newUserManager.getNewUserByAllKind(dateStart, dateEnd, "0", "0", "0");
		if (list1 != null && list1.size() > 0 && list1.get(0)[0] != null) {
			int sumNewAddUser = 0;
			for(Object[] o : list1) {
				sumNewAddUser += Integer.parseInt(o[1].toString());
			}
			o1.setValue(sumNewAddUser + "");
		}
		
		List<EntityDau> list2 = dauManager.getAllDauByAllKind(dateStart, dateEnd, "0", "0", "0");
		for(EntityDau o : list2) {
			o2.setValue(o.getDau() + "");
		}
		
		List<Object[]> list3  = totalCashManager.getTotalCashByAllKind(dateStart, dateEnd, "0", "0", "0");
		if (list3 != null && list3.size() > 0 && list3.get(0)[0] != null) {
			long sumCashAdd = 0;
			for(Object[] o : list3) {
				sumCashAdd += Long.parseLong(o[1].toString());
			}
			
			o3.setValue(UIHelper.getYuan(sumCashAdd) + "");
		}
		
		List<EntityPayUser> list4  = payUserManager.getAllPayUserByAllKind(dateStart, dateEnd, "0", "0", "0");
		for(EntityPayUser o : list4) {
			o4.setValue(o.getAddcash_user() + "");
		}
		
		if (!"-".equals(o4.getValue()) && !"-".equals(o2.getValue()) && !"0".equals(o2.getValue())) {
			o5.setValue(PageHelper.double2Percent(Integer.parseInt(o4.getValue()) * 1.0 / Integer.parseInt(o2.getValue())));
		} 

		if (!"-".equals(o4.getValue()) && !"-".equals(o3.getValue()) && !"0".equals(o4.getValue())) {
			o6.setValue(PageHelper.floatKeepTwoNumber((float)(Integer.parseInt(o3.getValue()) * 1.0 / Integer.parseInt(o4.getValue()))) + "");
		} 
		
		result.add(o1);
		result.add(o2);
		result.add(o3);
		result.add(o4);
		result.add(o5);
		result.add(o6);
		
		return result;
	}
	
	// 在线趋势
	public List<OnlineChartUIData> getOnlineChartUIData(Date dateStart, Date dateEnd) {
		Map<String, OnlineChartUIData> map1 = new LinkedHashMap<>();
		List<Object[]> list1 = acuManager.getAcuByAllKind(dateStart, dateEnd, "0", "0", "0");
		for(Object[] o : list1) {
			String key = o[0].toString();
			long value = Long.parseLong(o[1].toString());
			
			OnlineChartUIData tmp = map1.get(key);
			if (tmp == null) {
				tmp = new OnlineChartUIData();
				tmp.setDateStr(key);
				map1.put(key, tmp);
			}
			tmp.setAcu(value);
		}
		
		List<Object[]> list2 = pcuManager.getPcuByAllKind(dateStart, dateEnd, "0", "0", "0");
		for(Object[] o : list2) {
			String key = o[0].toString();
			int value = Integer.parseInt(o[1].toString());
			
			OnlineChartUIData tmp = map1.get(key);
			if (tmp == null) {
				tmp = new OnlineChartUIData();
				tmp.setDateStr(key);
				map1.put(key, tmp);
			}
			tmp.setPcu(value);
		}
		if (map1 != null && map1.size() > 0) {
			List<OnlineChartUIData> dataList1 = new ArrayList<OnlineChartUIData>(map1.values());
			Collections.sort(dataList1, new OnlineChartUIData().comparatorDate);
			return dataList1;
		} else {
			return null;
		}
	}
	
	// 留存
	public List<AliveChartUIData> getAliveChartUIData(Date dateStart, Date dateEnd) {
		Map<String, AliveChartUIData> map2 = new LinkedHashMap<>();
		List<Object[]> list3 = leftManager.getLeftByAllKind(dateStart, dateEnd, EntitySecondLeft.class, "0", "0", "0");
		for(Object[] o : list3) {
			String key = o[0].toString();
			float value = Float.parseFloat(o[1].toString());
			
			AliveChartUIData tmp = map2.get(key);
			if (tmp == null) {
				tmp = new AliveChartUIData();
				tmp.setDateStr(key);
				map2.put(key, tmp);
			}
			tmp.setThe2left(value);
		}
		List<Object[]> list4 = leftManager.getLeftByAllKind(dateStart, dateEnd, EntityThirdLeft.class, "0", "0", "0");
		for(Object[] o : list4) {
			String key = o[0].toString();
			float value = Float.parseFloat(o[1].toString());
			
			AliveChartUIData tmp = map2.get(key);
			if (tmp == null) {
				tmp = new AliveChartUIData();
				tmp.setDateStr(key);
				map2.put(key, tmp);
			}
			tmp.setThe3left(value);
		}
		List<Object[]> list5 = leftManager.getLeftByAllKind(dateStart, dateEnd, EntitySevenLeft.class, "0", "0", "0");
		for(Object[] o : list5) {
			String key = o[0].toString();
			float value = Float.parseFloat(o[1].toString());
			
			AliveChartUIData tmp = map2.get(key);
			if (tmp == null) {
				tmp = new AliveChartUIData();
				tmp.setDateStr(key);
				map2.put(key, tmp);
			}
			tmp.setThe7left(value);
		}
		if (map2 != null && map2.size() > 0) {
			List<AliveChartUIData> dataList2 = new ArrayList<AliveChartUIData>(map2.values());
			Collections.sort(dataList2, new AliveChartUIData().comparatorDate);
			return dataList2;
		} else {
			return null;
		}
	}
	
	// 活跃
	public List<ActiveChartUIData> getActiveChartUIData(Date dateStart, Date dateEnd) {
		Map<String, ActiveChartUIData> map3 = new LinkedHashMap<>();
		List<EntityDau> list6 = dauManager.getDauByAllKind(dateStart, dateEnd, "0", "0", "0");
		List<Object[]> list61 = newUserManager.getNewUserByAllKind(dateStart, dateEnd, "0", "0", "0");
		for(EntityDau o : list6) {
			String key = o.getStartday();
			float value = o.getDau();
			
			ActiveChartUIData tmp = map3.get(key);
			if (tmp == null) {
				tmp = new ActiveChartUIData();
				tmp.setDateStr(key);
				map3.put(key, tmp);
			}
			tmp.setOldActiveUser(value);
		}
		for(Object[] o : list61) {
			String key = o[0].toString();
			float value = Integer.parseInt(o[1].toString());
			
			ActiveChartUIData tmp = map3.get(key);
			if (tmp == null) {
				tmp = new ActiveChartUIData();
				tmp.setDateStr(key);
				tmp.setNewActiveUser(value);
				map3.put(key, tmp);
			} else {
				tmp.setNewActiveUser(value);
				tmp.setOldActiveUser(tmp.getOldActiveUser() - value);
			}
		}
		if (map3 != null && map3.size() > 0) {
			List<ActiveChartUIData> dataList3 = new ArrayList<ActiveChartUIData>(map3.values());
			Collections.sort(dataList3, new ActiveChartUIData().comparatorDate);
			return dataList3;
		} else {
			return null;
		}
	}
	
	// 付费相关
	public List<PayChartUIData> getPayChartUIData(Date dateStart, Date dateEnd) {
		Map<String, PayChartUIData> map5 = new LinkedHashMap<>();
		
		List<Object[]> list51  = totalCashManager.getTotalCashByAllKind(dateStart, dateEnd, "0", "0", "0");
		if (list51 != null && list51.size() > 0 && list51.get(0)[0] != null) {
			for(Object[] o : list51) {
				String key = o[0].toString();
				int value = Integer.parseInt(o[1].toString());
				
				PayChartUIData p = map5.get(key);
				if (p == null) {
					p = new PayChartUIData();
					p.setDateStr(key);
					map5.put(key, p);
				}
				p.setPay(value);
			}
		}
		List<Object[]> list52  = payUserManager.getPayUserByAllKind(dateStart, dateEnd, "0", "0", "0");
		if (list52 != null && list52.size() > 0 && list52.get(0)[0] != null) {
			for(Object[] o : list52) {
				String key = o[0].toString();
				int value = Integer.parseInt(o[1].toString());
				
				PayChartUIData p = map5.get(key);
				if (p == null) {
					p = new PayChartUIData();
					p.setDateStr(key);
					map5.put(key, p);
				}
				p.setPayUser(value);
			}
		}
		
		List<EntityDau> list53 = dauManager.getDauByAllKind(dateStart, dateEnd, "0", "0", "0");
		if (list53 != null && list53.size() > 0) {
			for(EntityDau o : list53) {
				String key = o.getStartday();
				int value = o.getDau();
				
				PayChartUIData p = map5.get(key);
				if (p == null) {
					p = new PayChartUIData();
					p.setDateStr(key);
					map5.put(key, p);
				}
				p.setDau(value);
			}
		}
		List<Object[]> list54  = newUserManager.getNewUserByAllKind(dateStart, dateEnd, "0", "0", "0");  
		if (list54 != null && list54.size() > 0 && list54.get(0)[0] != null) {
			for(Object[] o : list54) {
				String key = o[0].toString();
				int value = Integer.parseInt(o[1].toString());
				
				PayChartUIData p = map5.get(key);
				if (p == null) {
					p = new PayChartUIData();
					p.setDateStr(key);
					map5.put(key, p);
				}
				p.setNewUser(value);
			}
		}

        List<EntityNewUserLTV> list = newUserLTVManager.getAllNewUserLTVByAllKindOneDay(dateStart, dateEnd, "0", "0", "0");
        if (list != null && list.size() > 0) {
            for (EntityNewUserLTV ll : list) {
                String key = ll.getStartday();
                int value = (int)ll.getAddcash_user();

                PayChartUIData p = map5.get(key);
                if (p == null) {
                    p = new PayChartUIData();
                    p.setDateStr(key);
                    map5.put(key, p);
                }
                p.setNewPayUser(value);
            }
        }
        /*List<Object[]> list55  = firstPayUserManager.getFirstPayUserByAllKind(dateStart, dateEnd, "0", "0", "0");
		if (list55 != null && list55.size() > 0 && list55.get(0)[0] != null) {
			for(Object[] o : list55) {
				String key = o[0].toString();
				int value = Integer.parseInt(o[1].toString());
				
				PayChartUIData p = map5.get(key);
				if (p == null) {
					p = new PayChartUIData();
					p.setDateStr(key);
					map5.put(key, p);
				}
				p.setNewPayUser(value);
			}
		}*/
		
		for (Entry<String, PayChartUIData> entry : map5.entrySet()) {
			PayChartUIData v = entry.getValue();
			v.setPay(UIHelper.getYuan(v.getPay()));
			v.setPayRadio((float)(v.getPayUser() * 1.0 / (v.getDau() == 0 ? 1 : v.getDau())));
			v.setNewUserTodayPayRadio((float)(v.getNewPayUser() * 1.0 / (v.getNewUser() == 0 ? 1 : v.getNewUser())));
			v.setPayArpu(PageHelper.floatKeepTwoNumber((float)(v.getPay() * 1.0 / (v.getPayUser() == 0 ? 1 : v.getPayUser()))));
			v.setActiveUserApru(PageHelper.floatKeepTwoNumber((float)(v.getPay() * 1.0 / (v.getDau() == 0 ? 1 : v.getDau()))));
		}
		
		if (map5 != null && map5.size() > 0) {
			List<PayChartUIData> dataList5 = new ArrayList<PayChartUIData>(map5.values());
			Collections.sort(dataList5, new PayChartUIData().comparatorDate);
			return dataList5;
		} else {
			return null;
		}
	}
	
	// 游戏时长
	public List<PlayTimeChartUIData> getPlayTimeChartUIData(Date dateStart, Date dateEnd) {
		Map<String, PlayTimeChartUIData> map4 = new LinkedHashMap<>();
		List<Object[]> list7 = daPlayTimeManager.getDaPlayTimeByAllKind(dateStart, dateEnd, "0", "0", "0");
		for(Object[] o : list7) {
			String key = o[0].toString();
			float value = Integer.parseInt(o[1].toString());
			
			PlayTimeChartUIData tmp = map4.get(key);
			if (tmp == null) {
				tmp = new PlayTimeChartUIData();
				tmp.setDateStr(key);
				map4.put(key, tmp);
			}
			tmp.setPlayTime(value);
			tmp.setLoginCount((int)(Math.random() * 100) + 20);   // TODO
		}
		if (map4 != null && map4.size() > 0) {
			List<PlayTimeChartUIData> dataList4 = new ArrayList<PlayTimeChartUIData>(map4.values());
			Collections.sort(dataList4, new PlayTimeChartUIData().comparatorDate);
			return dataList4;
		} else {
			return null;
		}
	}
	
	/**
	 * 在线趋势chart数据类
	 * @author Administrator
	 *
	 */
	public class OnlineChartUIData {
		@ChartFlow(name = "日期")
		private String dateStr;
		@ChartSelect(name = "在线趋势", key = 1)
		@ChartData1(name = "pcu", key = 1)
		private long pcu;
		@ChartData1(name = "acu", key = 1)
		private long acu;
		
		// 根据日期排序的比较器
		public final Comparator<OnlineChartUIData> comparatorDate = new Comparator<OnlineChartUIData>() {
			public int compare(OnlineChartUIData s1, OnlineChartUIData s2) {
				return s2.getDateStr().compareTo(s1.getDateStr());
			}
		};

		public String getDateStr() {
			return dateStr;
		}

		public void setDateStr(String dateStr) {
			this.dateStr = dateStr;
		}

		public long getPcu() {
			return pcu;
		}

		public void setPcu(long pcu) {
			this.pcu = pcu;
		}

		public long getAcu() {
			return acu;
		}

		public void setAcu(long acu) {
			this.acu = acu;
		}
	}
	
	public class AliveChartUIData {
		@ChartFlow(name = "日期")
		private String dateStr;
		@ChartSelect(name = "留存率", key = 1)
		@ChartData1(name = "次日留存", key = 1)
		private float the2left;
		@ChartData1(name = "3日留存", key = 1)
		private float the3left;
		@ChartData1(name = "7日留存", key = 1)
		private float the7left;
		
		// 根据日期排序的比较器
		public final Comparator<AliveChartUIData> comparatorDate = new Comparator<AliveChartUIData>() {
			public int compare(AliveChartUIData s1, AliveChartUIData s2) {
				return s2.getDateStr().compareTo(s1.getDateStr());
			}
		};

		public String getDateStr() {
			return dateStr;
		}

		public void setDateStr(String dateStr) {
			this.dateStr = dateStr;
		}

		public float getThe2left() {
			return the2left;
		}

		public void setThe2left(float the2left) {
			this.the2left = the2left;
		}

		public float getThe3left() {
			return the3left;
		}

		public void setThe3left(float the3left) {
			this.the3left = the3left;
		}

		public float getThe7left() {
			return the7left;
		}

		public void setThe7left(float the7left) {
			this.the7left = the7left;
		}
	}
	
	public class ActiveChartUIData {
		@ChartFlow(name = "日期")
		private String dateStr;
		@ChartSelect(name = "活跃情况", key = 1)
		@ChartData1(name = "DAU-新用户", key = 1)
		private float newActiveUser;
		@ChartData1(name = "DAU-老用户", key = 1)
		private float oldActiveUser;
		
		// 根据日期排序的比较器
		public final Comparator<ActiveChartUIData> comparatorDate = new Comparator<ActiveChartUIData>() {
			public int compare(ActiveChartUIData s1, ActiveChartUIData s2) {
				return s2.getDateStr().compareTo(s1.getDateStr());
			}
		};

		public String getDateStr() {
			return dateStr;
		}

		public void setDateStr(String dateStr) {
			this.dateStr = dateStr;
		}

		public float getNewActiveUser() {
			return newActiveUser;
		}

		public void setNewActiveUser(float newActiveUser) {
			this.newActiveUser = newActiveUser;
		}

		public float getOldActiveUser() {
			return oldActiveUser;
		}

		public void setOldActiveUser(float oldActiveUser) {
			this.oldActiveUser = oldActiveUser;
		}

		public Comparator<ActiveChartUIData> getComparatorDate() {
			return comparatorDate;
		}
	}
	
	public class PayChartUIData {
		@ChartFlow(name = "日期")
		private String dateStr;
		@ChartSelect(name = "付费额", key = 1)
		@ChartData1(name = "付费额", key = 1)
		private int pay;
		@ChartSelect(name = "付费人数", key = 4)
		@ChartData1(name = "付费人数", key = 4)
		private int payUser;
		private int dau;
		@ChartSelect(name = "付费率&新增当日付费率", key = 2)
		@ChartData1(name = "付费率", key = 2)
		private float payRadio;
		private int newUser;
		private int newPayUser;
		@ChartData1(name = "新增当日付费率", key = 2)
		private float newUserTodayPayRadio;
		@ChartSelect(name = "付费ARPU&活跃ARPU", key = 3)
		@ChartData1(name = "付费ARPU", key = 3)
		private float payArpu;
		@ChartData1(name = "活跃ARPU", key = 3)
		private float activeUserApru;
		
		// 根据日期排序的比较器
		public final Comparator<PayChartUIData> comparatorDate = new Comparator<PayChartUIData>() {
			public int compare(PayChartUIData s1, PayChartUIData s2) {
				return s2.getDateStr().compareTo(s1.getDateStr());
			}
		};

		public String getDateStr() {
			return dateStr;
		}

		public void setDateStr(String dateStr) {
			this.dateStr = dateStr;
		}

		public int getPay() {
			return pay;
		}

		public void setPay(int pay) {
			this.pay = pay;
		}

		public int getPayUser() {
			return payUser;
		}

		public void setPayUser(int payUser) {
			this.payUser = payUser;
		}

		public float getPayRadio() {
			return payRadio;
		}

		public void setPayRadio(float payRadio) {
			this.payRadio = payRadio;
		}

		public float getNewUserTodayPayRadio() {
			return newUserTodayPayRadio;
		}

		public void setNewUserTodayPayRadio(float newUserTodayPayRadio) {
			this.newUserTodayPayRadio = newUserTodayPayRadio;
		}

		public float getPayArpu() {
			return payArpu;
		}

		public void setPayArpu(float payArpu) {
			this.payArpu = payArpu;
		}

		public float getActiveUserApru() {
			return activeUserApru;
		}

		public void setActiveUserApru(float activeUserApru) {
			this.activeUserApru = activeUserApru;
		}

		public Comparator<PayChartUIData> getComparatorDate() {
			return comparatorDate;
		}

		public int getDau() {
			return dau;
		}

		public void setDau(int dau) {
			this.dau = dau;
		}

		public int getNewUser() {
			return newUser;
		}

		public void setNewUser(int newUser) {
			this.newUser = newUser;
		}

		public int getNewPayUser() {
			return newPayUser;
		}

		public void setNewPayUser(int newPayUser) {
			this.newPayUser = newPayUser;
		}
	}
	
	public class PlayTimeChartUIData {
		@ChartFlow(name = "日期")
		private String dateStr;
		@ChartSelect(name = "游戏时长", key = 1)
		@ChartData1(name = "人均在线时长", key = 1)
		private float playTime;
		//@ChartData1(name = "人均登录次数", key = 1)
		private float loginCount;
		
		// 根据日期排序的比较器
		public final Comparator<PlayTimeChartUIData> comparatorDate = new Comparator<PlayTimeChartUIData>() {
			public int compare(PlayTimeChartUIData s1, PlayTimeChartUIData s2) {
				return s2.getDateStr().compareTo(s1.getDateStr());
			}
		};

		public String getDateStr() {
			return dateStr;
		}

		public void setDateStr(String dateStr) {
			this.dateStr = dateStr;
		}

		public float getPlayTime() {
			return playTime;
		}

		public void setPlayTime(float playTime) {
			this.playTime = playTime;
		}

		public float getLoginCount() {
			return loginCount;
		}

		public void setLoginCount(float loginCount) {
			this.loginCount = loginCount;
		}
	}
	
	// 日报汇总数据
	public class OverviewData {
		private String title;  // 标题
		private String value = "-";  // 数值

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			if (value != null && !"".equals(value)) {  // 为空时，不赋值，默认为“-”
				this.value = value;
			} 
		}
	}

    // 昨日汇总数据
    public class YesterDayOverviewData {
        private String title;  // 标题

        private String activeDevice = "-";  // 激活设备
        private String newDevice = "-";  // 新增设备
        private String newOnActiveDevcieRadio = "-";  // 激活新增转化率
        private String newUser = "-";  // 新增帐号
        private String activeUser = "-";  // 活跃帐号
        private String pay = "-"; // 付费金额
        private String payUser = "-";  // 付费帐号
        private String payOnActiveUserRadio = "-";  // 活跃付费率
        private String payArpu = "-";  // 付费ARPU
        private String activeArpu = "-";  // 活跃ARPU

        private String activeDeviceRadio = "-";  // 激活设备
        private String newDeviceRadio = "-";  // 新增设备
        private String newOnActiveDevcieRadioRadio = "-";  // 激活新增转化率
        private String newUserRadio = "-";  // 新增帐号
        private String activeUserRadio = "-";  // 活跃帐号
        private String payRadio = "-"; // 付费金额
        private String payUserRadio = "-";  // 付费帐号
        private String payOnActiveUserRadioRadio = "-";  // 活跃付费率
        private String payArpuRadio = "-";  // 付费ARPU
        private String activeArpuRadio = "-";  // 活跃ARPU

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getActiveDevice() {
            return activeDevice;
        }

        public void setActiveDevice(String activeDevice) {
            this.activeDevice = activeDevice;
        }

        public String getNewDevice() {
            return newDevice;
        }

        public void setNewDevice(String newDevice) {
            this.newDevice = newDevice;
        }

        public String getNewOnActiveDevcieRadio() {
            return newOnActiveDevcieRadio;
        }

        public void setNewOnActiveDevcieRadio(String newOnActiveDevcieRadio) {
            this.newOnActiveDevcieRadio = newOnActiveDevcieRadio;
        }

        public String getNewUser() {
            return newUser;
        }

        public void setNewUser(String newUser) {
            this.newUser = newUser;
        }

        public String getActiveUser() {
            return activeUser;
        }

        public void setActiveUser(String activeUser) {
            this.activeUser = activeUser;
        }

        public String getPayUser() {
            return payUser;
        }

        public void setPayUser(String payUser) {
            this.payUser = payUser;
        }

        public String getPayOnActiveUserRadio() {
            return payOnActiveUserRadio;
        }

        public void setPayOnActiveUserRadio(String payOnActiveUserRadio) {
            this.payOnActiveUserRadio = payOnActiveUserRadio;
        }

        public String getPayArpu() {
            return payArpu;
        }

        public void setPayArpu(String payArpu) {
            this.payArpu = payArpu;
        }

        public String getActiveArpu() {
            return activeArpu;
        }

        public void setActiveArpu(String activeArpu) {
            this.activeArpu = activeArpu;
        }

        public String getPay() {
            return pay;
        }

        public void setPay(String pay) {
            this.pay = pay;
        }

        public String getActiveDeviceRadio() {
            return activeDeviceRadio;
        }

        public void setActiveDeviceRadio(String activeDeviceRadio) {
            this.activeDeviceRadio = activeDeviceRadio;
        }

        public String getNewDeviceRadio() {
            return newDeviceRadio;
        }

        public void setNewDeviceRadio(String newDeviceRadio) {
            this.newDeviceRadio = newDeviceRadio;
        }

        public String getNewOnActiveDevcieRadioRadio() {
            return newOnActiveDevcieRadioRadio;
        }

        public void setNewOnActiveDevcieRadioRadio(String newOnActiveDevcieRadioRadio) {
            this.newOnActiveDevcieRadioRadio = newOnActiveDevcieRadioRadio;
        }

        public String getNewUserRadio() {
            return newUserRadio;
        }

        public void setNewUserRadio(String newUserRadio) {
            this.newUserRadio = newUserRadio;
        }

        public String getActiveUserRadio() {
            return activeUserRadio;
        }

        public void setActiveUserRadio(String activeUserRadio) {
            this.activeUserRadio = activeUserRadio;
        }

        public String getPayRadio() {
            return payRadio;
        }

        public void setPayRadio(String payRadio) {
            this.payRadio = payRadio;
        }

        public String getPayUserRadio() {
            return payUserRadio;
        }

        public void setPayUserRadio(String payUserRadio) {
            this.payUserRadio = payUserRadio;
        }

        public String getPayOnActiveUserRadioRadio() {
            return payOnActiveUserRadioRadio;
        }

        public void setPayOnActiveUserRadioRadio(String payOnActiveUserRadioRadio) {
            this.payOnActiveUserRadioRadio = payOnActiveUserRadioRadio;
        }

        public String getPayArpuRadio() {
            return payArpuRadio;
        }

        public void setPayArpuRadio(String payArpuRadio) {
            this.payArpuRadio = payArpuRadio;
        }

        public String getActiveArpuRadio() {
            return activeArpuRadio;
        }

        public void setActiveArpuRadio(String activeArpuRadio) {
            this.activeArpuRadio = activeArpuRadio;
        }
    }
}
